package com.zb.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.zb.entity.dto.LoginDto;
import com.zb.enums.TokenType;
import com.zb.exception.LoginException;
import com.zb.exception.SMSException;
import com.zb.redis.RedisLoginTokenUtil;
import com.zb.result.Result;
import com.zb.service.RefreshTokenService;
import com.zb.service.ZjAdmAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author WEN
 * @Date 2023/7/10 0010
 **/
@Api(tags = "登录类接口")
@RestController
@RequestMapping("/adm")
public class LoginController {
    @Autowired
    private ZjAdmAccountService admAccountService;
    @Autowired
    private RefreshTokenService refreshTokenService;

    @ApiOperation(value = "登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody LoginDto loginDto) throws LoginException, SMSException {
        return Result.success("登录成功").data("row", admAccountService.login(loginDto));
    }

    @ApiOperation(value = "刷新token")
    @GetMapping("/refreshToken")
    public Result refreshToken(String refreshToken) throws LoginException {
        return Result.success("登录成功").data("row", refreshTokenService.refreshToken(refreshToken));
    }

    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public Result logout(HttpServletRequest request) {
        String accessToken = request.getHeader(TokenType.ACCESS_TOKEN.getType());
        RedisLoginTokenUtil.removeAllTokenByAccessToken(accessToken);
        StpUtil.logoutByTokenValue(accessToken);
        return Result.success("退出登录成功！");
    }
}
